<?php
class ModelProductCategory extends Model {
	public function addCategory($data) {
		$name = $this->db->escape($data['name']);
		$sql = "INSERT INTO " . DB_PREFIX . "category SET parent_id = '" . (int)$data['parent_id'] . 
			"', name = '" . $name;
		if($data['image']){
			$sql .= "', image = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8'));
		}
		$sql .= "', layout_id = '" . (int)$data['layout_id'] . 
			"', sort_order = '" . (int)$data['sort_order'] . 
			"', status = '" . (int)$data['status'] . 
			"', date_added = UNIX_TIMESTAMP(), date_modified = UNIX_TIMESTAMP()";
		$this->db->query($sql);	
		$category_id = $this->db->getLastId();
		
		foreach ($data['category_description'] as $language_id => $value) {
			$sql_desc = "INSERT INTO " . DB_PREFIX . "category_description SET category_id = '" . (int)$category_id . 
				"', language_id = '" . (int)$language_id . 
				"', display_name = '";
			if($value['display_name']){
				$sql_desc .= $this->db->escape($value['display_name']);
			}else{
				$sql_desc .= $name;
			}
			$sql_desc .= "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . 
				"', meta_description = '" . $this->db->escape($value['meta_description']) . 
				"', tag = '" . $this->db->escape($value['tag']) . 
				"', description = '" . $this->db->escape($value['description']) . "'";
			$this->db->query($sql_desc);
		}		
		$this->cache->delete('category');
	}
	
	public function editCategory($category_id, $data) {
		$name = $this->db->escape($data['name']);
		$sql = "UPDATE " . DB_PREFIX . "category SET parent_id = '" . (int)$data['parent_id'] . 
			"', name = '" . $name;
		if($data['image']){
			$sql .= "', image = '" . $this->db->escape(html_entity_decode($data['image'], ENT_QUOTES, 'UTF-8')) . "'";
		}else{
			$sql .= "', image = null";
		}
		$sql .= ", layout_id = '" . (int)$data['layout_id'] . 
			"', sort_order = '" . (int)$data['sort_order'] . 
			"', status = '" . (int)$data['status'] . 
			"', date_modified = UNIX_TIMESTAMP() WHERE category_id = '" . (int)$category_id . "'";
		$this->db->query($sql);	
		
		$this->db->query("DELETE FROM " . DB_PREFIX . "category_description WHERE category_id = '" . (int)$category_id . "'");
		foreach ($data['category_description'] as $language_id => $value) {
			$sql_desc = "INSERT INTO " . DB_PREFIX . "category_description SET category_id = '" . (int)$category_id . 
				"', language_id = '" . (int)$language_id . 
				"', display_name = '";
			if($value['display_name']){
				$sql_desc .= $this->db->escape($value['display_name']);
			}else{
				$sql_desc .= $name;
			}
			$sql_desc .= "', meta_keyword = '" . $this->db->escape($value['meta_keyword']) . 
				"', meta_description = '" . $this->db->escape($value['meta_description']) . 
				"', tag = '" . $this->db->escape($value['tag']) . 
				"', description = '" . $this->db->escape($value['description']) . "'";
			$this->db->query($sql_desc);
		}
		$this->cache->delete('category');
	}
	
	public function editCategorySortOrder($data) {
		foreach ($data['category'] as $category_id => $value) {
			$this->db->query("UPDATE " . DB_PREFIX . "category SET sort_order = '" . (int)$value['sort_order'] . 
				"' WHERE category_id = '" . (int)$category_id . "'");
		}
		$this->cache->delete('category');
	}

	public function getCategory($category_id) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category WHERE category_id = '" . (int)$category_id . "'");
		return $query->row;
	}
	
	public function getCategoryDescriptions($category_id) {
		$category_description_data = array();
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "category_description WHERE category_id = '" . (int)$category_id . "'");
		foreach ($query->rows as $result) {
			$category_description_data[$result['language_id']] = array(
				'category_id'      => $result['category_id'],
				'language_id'      => $result['language_id'],
				'display_name'     => $result['display_name'],
				'meta_keyword'     => $result['meta_keyword'],
				'meta_description' => $result['meta_description'],
				'tag'              => $result['tag'],
				'description'      => $result['description']
			);
		}
		return $category_description_data;
	}
		
	public function getCategories($category_id = 0) {
		$language_id = (int)$this->config->get('config_language_id');
		$category_data = array();
		if($category_id > 0){
			$query = $this->db->query("SELECT c.category_id, c.`name`, c.sort_order, c.`status`, cd.display_name FROM " . 
				DB_PREFIX . "category c LEFT JOIN " . 
				DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id AND cd.language_id = '" . (int)$language_id . 
				"') WHERE c.category_id = '" . (int)$category_id . "'");
		}else{
			$query = $this->db->query("SELECT c.category_id, c.`name`, c.sort_order, c.`status`, cd.display_name FROM " . 
				DB_PREFIX . "category c LEFT JOIN " . 
				DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id AND cd.language_id = '" . (int)$language_id . 
				"') WHERE c.parent_id = '" . (int)$category_id . "' ORDER BY c.sort_order ASC, cd.display_name ASC");
		}
		return $query->rows;
	}
		
	public function getCategoriesByPid($parent_id = 0) {
		$language_id = (int)$this->config->get('config_language_id');
		$category_data = array();
		$query = $this->db->query("SELECT c.category_id, c.`name`, c.sort_order, c.`status`, cd.display_name FROM " . 
			DB_PREFIX . "category c LEFT JOIN " . 
			DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id AND cd.language_id = '" . (int)$language_id . 
			"')  WHERE c.parent_id = '" . (int)$parent_id . "' ORDER BY c.sort_order ASC, cd.display_name ASC");
		return $query->rows;
	}
	
	public function getAllCategoriesForSelect($parent_id = 0, $parent_name = '', $parent_display_name = '', $status = -1) {
		$language_id = (int)$this->config->get('config_language_id');
		if($parent_name != ''){
			$parent_name .= '>>';
		}
		if($parent_display_name != ''){
			$parent_display_name .= '>>';
		}
		$sql = "SELECT c.category_id, c.`name`, c.`status`, cd.display_name FROM " . 
			DB_PREFIX . "category c LEFT JOIN " . 
			DB_PREFIX . "category_description cd ON (c.category_id = cd.category_id AND cd.language_id = '" . (int)$language_id . 
			"') WHERE c.parent_id = '" . (int)$parent_id;
		if($status > -1){
			$sql .= "' AND `status` = '" . (int)$status . "' ORDER BY c.sort_order ASC, cd.display_name ASC";
		}else{
			$sql .= "' ORDER BY c.sort_order ASC, cd.display_name ASC";
		}
		$query = $this->db->query($sql);
		$category_data = array();
		foreach ($query->rows as $result) {
			$category_data[] = array(
				'category_id' => $result['category_id'],
				'name'        => $parent_name . $result['name'],
				'display_name'=> $parent_display_name . $result['display_name'],
				'status'      => $result['status']
			);
			$category_data = array_merge($category_data, $this->getAllCategoriesForSelect($result['category_id'], $parent_name . $result['name'], $parent_display_name . $result['display_name'], $status));
		}
		return $category_data;
	}
}
?>